<?php
namespace Nawa\MedicalStudy;
/**
 * Medical Center Partner of a medical study.
 *
 * It represant the link betzeen a Medical Study and a Medical Center
 *
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy
 * @license
 *
 * @property int	$msid			Id of the medical study
 * @property int	$mcid			Medical Center
 * 
 * @method array documents()	Retrieve all attached documents
 * @method array patients()		Retrieve all attached patients
 * @method array partners() 	Retrieve all attached partners 
 *  
 */
use Fox\Dao\SqlFragment;

class MedicalCenter extends \Fox\Object {
	
	/**
	 * Join Customers
	 * @var array
	 */
	public static $JOIN_CUSTOMERS = array(
		'source' => 'customers',
		'table' => 'ms_mc_partners',
		'short' => 'pa_mc',
		'foreign' => 'customerid',
		'column' => 'customerid',
		'type' => 'INNER JOIN');
	
	/**
	 * Link to Attached Documents (Link Table not the real Document Object)
	 */
	const LINK_DOCUMENTS = 'documents';

	/**
	 * Link to Attached Patients
	 */
	const LINK_PATIENTS = 'patients';

	/**
	 * Link to Attached Partners
	 */
	const LINK_PARTNERS = 'partners';

	/**
	 * Link to Attached Partner
	 */
	const LINK_PARTNER = 'partner';
	
	/**
	 * Link to Attached Partners
	 */
	const LINK_MEDICAL_CENTER = 'medicalCenter';

	/**
	 * Link to Attached Medical Study
	 */
	const LINK_MEDICAL_STUDY = 'medicalStudy';

	/**
	 * Link to Attached Medical Study
	 */
	const LINK_MONITORINGS = 'monitorings';
	
	/**
	 * Grouping Type of Documents
	 * Use to filter the list of available documents
	 * @var string
	 */
	const DOC_GROUPING = 'mc';

	/**
	 * Retrieve partner link & data has an array
	 * @return array
	 */
	public function partnersData() {
		$data = array();
		$handle = \Fox\Dao::infos(self::className())->handle();
		$query = $handle->createQueryBuilder();
		$query->select('t.msid','t.mcid','t.customerid','msp.role','msp.docid','c.name1','c.name2','c.docid AS base_docid')
				->from('ms_mc_partners','t')
				->innerJoin('t','ms_partners','msp','msp.msid = t.msid AND msp.customerid = t.customerid')
				->innerJoin('t','customers','c','t.customerid = c.customerid');
		$fragments[] = new SqlFragment('t.msid = ? AND t.mcid = ?',array($this->msid,$this->mcid));
		$fragment = SqlFragment::combine($fragments);
		$results = $fragment->pushIn($query)->execute()->fetchAll(\PDO::FETCH_ASSOC);
		foreach($results as $item)
		{
			$item['medicalcenters'] = null;
			$centers = \Fox\Dao::finder(\Nawa\MedicalCenter::className())
				->follow(\Nawa\MedicalCenter::LINK_MEDICAL_STUDIES_CENTER_PARTNERS,\Fox\Object\Link::FETCH_LAZY)
				->where('t.msid = ? AND t.mcid != ? AND customerid = ?',array($this->msid,$this->mcid,$item['customerid']))
				->find();
		
			foreach($centers as $center) {
				$item['medicalcenters'][] = array('label' => $center->name1,'mcid' => $center->mcid);
			}
			if(!empty($item['medicalcenters'])) {
				$item['medicalcenters'] = preg_replace("/\"/","'",json_encode($item['medicalcenters']));
			}
			$data[$item['customerid']] = $item;
		}
		return $data;
	}

}
\Fox\Dao::infos(MedicalCenter::className(),new \Fox\Object\Info('ms_mcs', \Fox\Databases::DB_APP));

// Link to Documents attached to the Medical Center Partner
\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter\Document::className(),
MedicalCenter\Document::LINK_MEDICAL_CENTER)
->to(MedicalCenter::className(), MedicalCenter::LINK_DOCUMENTS)
->on('msid','mcid')
->versionFrom());

// Link to Patiens attached to the Medical Center Partner
\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter\Patient::className(),
MedicalCenter\Patient::LINK_MEDICAL_CENTER)
->to(MedicalCenter::className(), MedicalCenter::LINK_PATIENTS)
->on('msid','mcid')
->versionFrom());

// Link to Partners attached to the Medical Center Partner
\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter\Partner::className(),
MedicalCenter\Partner::LINK_MEDICAL_CENTER_PARTNER)
->to(MedicalCenter::className(), MedicalCenter::LINK_PARTNERS)
->on('msid','mcid')
->versionFrom());

// Link to Monitorings attached to the Medical Center
\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter\Monitoring::className(),MedicalCenter\Monitoring::LINK_MEDICAL_CENTER_PARTNER)
->to(MedicalCenter::className(), MedicalCenter::LINK_MONITORINGS)
->on('msid','mcid')
->versionFrom());

// Link to Medical Center Object
\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter::className(),MedicalCenter::LINK_MEDICAL_CENTER)
->to(\Nawa\MedicalCenter::className(), \Nawa\MedicalCenter::LINK_MEDICAL_STUDIES)
->on('mcid'));

// Link to Medical Center Object
\Nawa\MedicalCenter::className();
// Link to Medical Center Object
\Nawa\MedicalStudy::className();